using System;
using FubuMVC.Core.Runtime;

namespace FubuMVC.Core.Behaviors
{
    public class call_OMIOMO_action<TController, TInput, TOutput> : IActionBehavior
        where TInput : class
    {
        private readonly IModelState _modelState;
        private readonly TController _controller;
        private readonly Func<TController, TInput, TOutput> _controllerAction;

        public call_OMIOMO_action(IModelState modelState, TController controller, Func<TController, TInput, TOutput> controllerAction)
        {
            _modelState = modelState;
            _controller = controller;
            _controllerAction = controllerAction;
        }

        public IActionBehavior InsideBehavior { get; set; }

        public void Invoke()
        {
            var input = _modelState.RetrieveModel<TInput>();
            var output = _controllerAction(_controller, input as TInput);
            _modelState.ProvideModel(output);
        }
    }
}